<template>
  <div class="wrapper">
    <div class="search">
      <el-input
        v-model="searchKeyword"
        placeholder="搜索餐厅"
        class="input"
      ></el-input>
      <el-input
        v-model="searchAddress"
        placeholder="搜索地址"
        class="input"
      ></el-input>
      <el-input
        v-model="searchPhone"
        placeholder="搜索电话"
        class="input"
      ></el-input>
    </div>
    <div class="restlist">
      <div
        class="restItem"
        v-for="item in filteredRestaurants"
        :key="item.restaurant_id"
        @click="goToDetails(item.restaurant_id)"

      >
        <div class="mainImg">
          <img
            :src="item.image_url"
            class="image"
            alt="餐厅图片"
            title="查看餐厅菜单"
          />
        </div>
        <div class="details">
          <div class="name">
            <div class="label">餐厅名：</div>
            <div class="val">{{ item.restaurant_name }}</div>
          </div>
          <div class="address">
            <div class="label">地址：</div>
            <div class="val">{{ item.address }}</div>
          </div>
          <div class="phone">
            <div class="label">电话：</div>
            <div class="val">{{ item.contact_number }}</div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from "vue";
import { restaurantApi } from "@/utils/api";
import { useRouter } from "vue-router";
const router = useRouter();

const state = ref([]);
const searchKeyword = ref("");
const searchAddress = ref("");
const searchPhone = ref("");

const init = async () => {
  const res = await restaurantApi();
  if (res.code === 200) {
    const filteredData = res.data.filter(item => item.status !== 0);
    state.value = filteredData.map(item => ({
      ...item,
      image_url: `http://localhost:3000/images/rest/${item.image_url}`
    }));
  }
};


init();

const filteredRestaurants = computed(() => {
  return state.value.filter(
    (restaurant) =>
      restaurant.restaurant_name
        .toLowerCase()
        .includes(searchKeyword.value.toLowerCase()) &&
      restaurant.address
        .toLowerCase()
        .includes(searchAddress.value.toLowerCase()) &&
      restaurant.contact_number
        .toLowerCase()
        .includes(searchPhone.value.toLowerCase())
  );
});

const goToDetails = (id) => {
  console.log(id);
  router.push(`/details/${id}`);
};
</script>

<style lang="less" scoped>
.wrapper {
  width: 100%;
  height: 100%;
  box-sizing: border-box;
  position: relative;
  display: flex;
  flex-direction: column;
  .search {
    flex-shrink: 0;
    padding: 10px 20px 0;
  }
  .input {
    margin-right: 10px;
    width: 200px;
    height: 30px;
    border-radius: 5px;
    border: none;
    margin-bottom: 10px;
  }
  .restlist {
    flex: 1;
    overflow: auto;
    padding: 20px;
    // display: flex;
    // flex-wrap: wrap;
    // justify-content: space-evenly;
    display: grid;
    grid-template-columns: repeat(5, 1fr); /* 创建 5 列 */
    gap: 20px; /* 间隔，可根据需要调整 */
    padding-top: 10px;
    .restItem {
      box-shadow: 1px 1px 10px 1px rgba(0, 0, 0, 0.3);
      // margin-bottom: 15px;
      cursor: pointer;
      border-radius: 15px;
      display: flex;
      justify-content: center;
      padding: 15px;
      box-sizing: border-box;
      flex-wrap: wrap;
      // width: 23%;
      height: 250px;

      .mainImg {
        width: 150px; /* 容器宽度 */
        height: 150px; /* 容器高度 */
        overflow: hidden; /* 超出部分隐藏 */
      }

      .mainImg img {
        width: 100%; /* 图片宽度占满容器 */
        height: 100%; /* 图片高度占满容器 */
        object-fit: cover;
        transition: transform 0.3s; /* 移除 transition 属性 */
      }

      .mainImg img:hover {
        transform: scale(1.4);
      }

      .details {
        margin-top: 5px;
        > div {
          width: 100%;
          display: flex;
          line-height: 1.5em;
          font-size: 15px;
          .label {
            color: #999;
            width: 4em;
            text-align: justify;
            text-align-last: justify;
          }
        }
      }
    }
  }
}
.main {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-evenly;
}
</style>
